﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace PersonaLibrary.Search
{
	public class DateSearchCriteria : SearchCriteria
	{
		private readonly Dictionary<string, int> _mounth = new Dictionary<string, int>(12);

		public DateSearchCriteria()
		{
			_mounth.Add("январь", 1);
			_mounth.Add("февраль", 2);
			_mounth.Add("март", 3);
			_mounth.Add("апрель", 4);
			_mounth.Add("май", 5);
			_mounth.Add("июнь", 6);
			_mounth.Add("июль", 7);
			_mounth.Add("август", 8);
			_mounth.Add("сентябрь", 9);
			_mounth.Add("октябрь", 10);
			_mounth.Add("ноябрь", 11);
			_mounth.Add("декабрь", 12);
		}
		
		public override void CreateWhere(StringBuilder stringBuilder)
		{
			string[] splittedValue = new [] { Value };

			if (Regex.IsMatch(Value, @"(\d{1}|\d{2})\.(\d{1}|\d{2})\.\d{4}"))
			{
				splittedValue = Value.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
				CreateDay(stringBuilder, splittedValue[0]);
				stringBuilder.Append(" and ");
				CreateMonth(stringBuilder, int.Parse(splittedValue[1]));
				stringBuilder.Append(" and ");
				CreateYear(stringBuilder, splittedValue[2]);
				return;
			}

			if (Value.Contains('.'))
				splittedValue = Value.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
			else if(Value.Contains(' '))
				splittedValue = Value.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
			else if (Value.Contains('/'))
				splittedValue = Value.Split("/".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

			for (int i = 0; i < splittedValue.Length; i++ )
			{
				CreateValue(stringBuilder, splittedValue[i]);

				if(i != splittedValue.Length - 1)
					stringBuilder.Append(" and ");
			}
		}

		private void CreateValue(StringBuilder stringBuilder, string value)
		{
			if (Regex.IsMatch(value, @"([а-я]|[А-Я])+"))
				CreateMonth(stringBuilder, value);
			else if (value.Length == 4)
				CreateYear(stringBuilder, value);
			else CreateDay(stringBuilder, value);
		}

		private void CreateMonth(StringBuilder stringBuilder, string month)
		{
			stringBuilder.Append("Month(");
			stringBuilder.Append(PropertyName);
			stringBuilder.Append(")");
			stringBuilder.Append(" ");
			stringBuilder.Append(Operator);
			stringBuilder.Append(" ");
			string key = _mounth.Keys.FirstOrDefault(x => x.Equals(month.ToLower()));
			int monthNumber = _mounth[key];
			stringBuilder.Append(monthNumber);
		}

		private void CreateMonth(StringBuilder stringBuilder, int month)
		{
			stringBuilder.Append("Month(");
			stringBuilder.Append(PropertyName);
			stringBuilder.Append(")");
			stringBuilder.Append(" ");
			stringBuilder.Append(Operator);
			stringBuilder.Append(" ");
			stringBuilder.Append(month);
		}

		private void CreateYear(StringBuilder stringBuilder, string year)
		{
			stringBuilder.Append("Year(");
			stringBuilder.Append(PropertyName);
			stringBuilder.Append(")");
			stringBuilder.Append(" ");
			stringBuilder.Append(Operator);
			stringBuilder.Append(" ");
			stringBuilder.Append(year);
		}

		private void CreateDay(StringBuilder stringBuilder, string day)
		{
			stringBuilder.Append("Day(");
			stringBuilder.Append(PropertyName);
			stringBuilder.Append(")");
			stringBuilder.Append(" ");
			stringBuilder.Append(Operator);
			stringBuilder.Append(" ");
			stringBuilder.Append(day);
		}
	}
}
